import {
    getSiderBar
} from "@/api/core/system";
import store from '@/utils/store/cache'
import { queryTree } from "@/utils/index";
import { set } from "@/store/utils/mutations";
import { dbSet, dbGet } from "@/store/utils/action";

export default {
    namespaced: true,
    state: {
        // 权限数据
        access: [],
        // 菜单数据
        aside: []
    },
    actions: {
        dbSet,
        dbGet,
        /**
         * 请求用户权限数据
         * @param {Object} context
         */
        async accessLoad(context: any) {
            store.loadCacheDataByFun(context, 'aside', getSiderBar, context.rootState.account.info.id, {
                mutationName: 'aside'
            });
        }
    },
    getters: {
        // 权限数据
        access(state: any) {
            return state.access;
        },
        // 菜单数据
        aside(state: any) {
            // console.log(state.aside);
            return state.aside;
        }
    },
    mutations: {
        set,
        aside(state: any, params: any) {
            const treeData = params.data;
            state.access = treeData;
            // 过滤掉权限数据
            state.aside = queryTree(treeData, function (item: any) {
                return item.menuType == 'C';
            });
            // console.log(state.aside)
        }
    }
}